package cn.jinjimi.base.preference;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by Roy
 * Date: 15/11/25
 */
public class PreferenceManager {
    private static Application mApplication;

    public static void init(Application app) {
        mApplication = app;
    }

    /**
     * CAUTION:
     * old preference key: "settings", "encourage_pic"
     * new preference key: SharePrefKeys.PREF_XRK_NAME = "sixapp_app_config_file"
     *
     * @return
     */
    private static SharedPreferences getUserPreferences() {
        return mApplication.getSharedPreferences(PrefTable.PREF_XRK_NAME, Context.MODE_PRIVATE);
    }

    private static SharedPreferences getPreference(String tableName) {
        return mApplication.getSharedPreferences(tableName, Context.MODE_PRIVATE);
    }

    public static void setData(String key, Object object) {
        setData(PrefTable.PREF_XRK_NAME, key, object);
    }

    public static void setData(String tableName, String key, Object object) {
        if (object == null)
            return;

        SharedPreferences.Editor editor = null;

        try {
            editor = getPreference(tableName).edit();

            if (object instanceof String) {
                editor.putString(key, (String) object);
            } else if (object instanceof Integer) {
                editor.putInt(key, (Integer) object);
            } else if (object instanceof Boolean) {
                editor.putBoolean(key, (Boolean) object);
            } else if (object instanceof Long) {
                editor.putLong(key, (Long) object);
            } else if (object instanceof Float) {
                editor.putFloat(key, (Float) object);
            }

        } catch (Exception e) {
            //
        } finally {
            if (editor != null) {
                editor.apply();
            }
        }
    }

    public static void removeData(String key) {
        try {
            SharedPreferences.Editor editor = getUserPreferences().edit();
            editor.remove(key);
            editor.commit();
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    public static String getStringData(String key) {
        try {
            return getUserPreferences().getString(key, "");
        } catch (RuntimeException e) {
            return "";
        }
    }

    public static int getIntData(String key) {
        try {
            return getUserPreferences().getInt(key, 0);
        } catch (RuntimeException e) {
            return 0;
        }
    }

    public static int getSpecialIntData(String key, int special) {
        try {
            return getUserPreferences().getInt(key, special);
        } catch (RuntimeException e) {
            return special;
        }
    }

    public static long getLongData(String key) {
        try {
            return getUserPreferences().getLong(key, 0);
        } catch (RuntimeException e) {
            return 0;
        }
    }

    public static Float getFloatData(String key) {
        try {
            return getUserPreferences().getFloat(key, 0f);
        } catch (RuntimeException e) {
            return 0f;
        }
    }

    public static boolean getBooleanData(String key) {
        try {
            return getBooleanData(key, false);
        } catch (RuntimeException e) {
            return false;
        }
    }

    public static boolean getBooleanData(String key, boolean defValue) {
        try {
            return getUserPreferences().getBoolean(key, defValue);
        } catch (RuntimeException e) {
            return defValue;
        }
    }

    public static void cleanUserPreference() {
        try {
            SharedPreferences.Editor editor = getUserPreferences().edit();
            editor.clear();
            editor.commit();
        } catch (RuntimeException e) {
            //do nothing
        }
    }

}
